package com.ayoka.parenting.mvp;

import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: MVPBaseFragment.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public abstract class MVPBaseFragment<VDB extends ViewDataBinding, V extends BaseView, P extends BasePresenter> extends Fragment implements BaseView, View.OnClickListener{

    protected VDB mDataBinding;

    protected String TAG;

    // 参数在fragment可见时通过系统回调setUserVisibleHint方法设置为true,不可见时为false，这是fragment实现懒加载的关键。
    private boolean isVisible = true;
    // 参数在系统调用onActivityCreated时设置为true,这时onCreateView方法已调用完毕(一般我们在这方法里执行findViewById等方法),确保 initData()方法不会报空指针异常。
    private boolean isActivityCreated;
    // 确保ViewPager来回切换时BaseFragment的initData方法不会被重复调用，initData在该Fragment的整个生命周期只调用一次,第一次调用initData()方法后马上执行 isFirst = false。
    private boolean isFirst = true;

    protected P mPresenter;

    public void setFirst(boolean first) {
        isFirst = first;
    }

    public boolean isFirst() {
        return isFirst;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (isVisible) {
            lazyLoad();
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isActivityCreated = true;
        lazyLoad();
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        TAG = getClass().getSimpleName();
        configBundleArguments();
        mPresenter = getPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
        super.onCreate(savedInstanceState);
    }

    protected void configBundleArguments() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getContentLayout(), null);
        mDataBinding = DataBindingUtil.bind(view);
        //初始化事件和获取数据, 在此方法中获取数据不是懒加载模式
        initViews(view);
        return view;
    }

    protected abstract int getContentLayout();

    protected abstract void initViews(View view);

    protected abstract void initData();

    protected abstract P getPresenter();

    public void lazyLoad() {
        Log.d(TAG, "lazyLoad" + isActivityCreated + isVisible + isFirst);
        if (!isActivityCreated || !isVisible || !isFirst) {
            return;
        }
        initData();
        isFirst = false;
    }

    @Override
    public Context getContext() {
        return getActivity();
    }


    /**
     * 隐藏对话框
     */
    @Override
    public void hideWaitDialog() {
        Activity activity = getActivity();
        if (activity instanceof BaseView) {
            ((BaseView) activity).hideWaitDialog();
        }
    }

    /**
     * 显示一个对话框 默认使用R.string.loading
     *
     */
    public void showWaitDialog() {
        showWaitDialog("正在加载中...");
    }

    /**
     * 显示一个对话框
     *
     * @param message 使用系统中的自定义的文字显示
     */
    @Override
    public void showWaitDialog(String message) {
        Activity activity = getActivity();
        if (activity instanceof BaseView) {
           ((BaseView) activity).showWaitDialog(message);
        }
    }

    @Override
    public void showToast(String message) {
        Activity activity = getActivity();
        if (activity instanceof BaseView) {
           ((BaseView) activity).showToast(message);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        setUserVisibleHint(!hidden);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hideWaitDialog();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
    }

    @Override
    public void onClick(View view) {
    }
}
